<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.4.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">


<link rel="stylesheet" href="/lib/font-awesome/css/font-awesome.min.css">

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"zzcz.cc","root":"/","scheme":"Pisces","version":"7.8.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":true,"show_result":false,"style":null},"back2top":{"enable":true,"sidebar":false,"scrollpercent":false},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":false,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}}};
  </script>

  <meta name="description" content="点击看详情">
<meta property="og:type" content="article">
<meta property="og:title" content="语言国际化">
<meta property="og:url" content="http://zzcz.cc/4f5e528e.html">
<meta property="og:site_name" content="茁壮成长">
<meta property="og:description" content="点击看详情">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2020-02-24T12:53:22.000Z">
<meta property="article:modified_time" content="2020-02-25T12:58:48.000Z">
<meta property="article:author" content="YAND">
<meta property="article:tag" content="iOS">
<meta name="twitter:card" content="summary">

<link rel="canonical" href="http://zzcz.cc/4f5e528e.html">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>语言国际化 | 茁壮成长</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">茁壮成长</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
      <p class="site-subtitle" itemprop="description">人生在世 难得糊涂</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="menu">
        <li class="menu-item menu-item-home">

    <a href="/" rel="section"><i class="fa fa-fw fa-home"></i>首页</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/tags/" rel="section"><i class="fa fa-fw fa-tags"></i>标签</a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/categories/" rel="section"><i class="fa fa-fw fa-th"></i>分类</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="fa fa-fw fa-archive"></i>归档</a>

  </li>
        <li class="menu-item menu-item-about">

    <a href="/about/" rel="section"><i class="fa fa-fw fa-user"></i>关于</a>

  </li>
  </ul>
</nav>




</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://zzcz.cc/4f5e528e.html">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.jpg">
      <meta itemprop="name" content="YAND">
      <meta itemprop="description" content="成长历程 IT笔记 黑苹果 杂谈 建站 服务器 VPS">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="茁壮成长">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          语言国际化
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2020-02-24 20:53:22" itemprop="dateCreated datePublished" datetime="2020-02-24T20:53:22+08:00">2020-02-24</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="fa fa-calendar-check-o"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2020-02-25 20:58:48" itemprop="dateModified" datetime="2020-02-25T20:58:48+08:00">2020-02-25</time>
              </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/iOS/" itemprop="url" rel="index"><span itemprop="name">iOS</span></a>
                </span>
            </span>

          

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <center>点击看详情</center>

<span id="more"></span>

<p><a target="_blank" rel="noopener" href="http://blog.csdn.net/huangmindong/article/details/54848282">http://blog.csdn.net/huangmindong/article/details/54848282</a><br>语言本地化，又叫做语言国际化。是指根据用户操作系统的语言设置，自动将应用程序的语言设置为和用户操作系统语言一致的语言。往往一些应用程序需要提供给多个国家的人群使用，或者一个国家有多种语言，这就要求应用程序所展示的文字、图片等信息，能够让讲不同语言的用户读懂、看懂。进而提出为同一个应用程序适配多种语言，也即是国际化。语言国际化之所以又叫做语言本地化，这是站在每个用户的角度而言的，是指能够让用户本地、本土人群能够看懂的语言信息，顾名思义，语言本地化。其实语言本地化 == 语言国际化！</p>
<p>本文将分如下7个主要章节一步一步讲解如何完全本地化一个App。</p>
<p>配置需要国际化的语言（国际化的准备工作）<br>App名称本地化<br>代码中字符串本地化<br>多人开发情况下的字符串本地化<br>图片本地化（两种方式两种方式）<br>查看/切换本地语言<br>storyboard/xib本地化<br>配置需要国际化的语言</p>
<p>配置需要国际化的语言，这也是国际化之前的准备工作，无论我们是国际化App名称、代码中的字符串、图片、还是storyboard和xib，都需要进行这一步的准备工作（一个项目中需要且仅需要配置一次）。</p>
<p>选中project-&gt;Info-&gt;Localizations，然后点击”+”，添加需要国际化/本地化的语言，如下图（默认需要勾选Use Base Internationalization）：</p>
<p>1055199-9929ad6b38dae886.png</p>
<p>此处以添加法语为例，如下图：</p>
<p>1055199-5b8ec0ce8c5f61e7.png</p>
<p>弹出如下对话框，直接点击finish，如下图：</p>
<p>1055199-3aa4a15594e1684d.png</p>
<p>同理，添加简体中文、繁体中文、韩语，最终结果如下图：</p>
<p>1055199-ce7709e58803ed19.png</p>
<p>备注： “zh-Hans”和“zh-Hant”是简体中文和繁体中文的缩写。这是标准的缩写。H可大写也可小写。”en”是英语的缩写。ko是韩语的缩写，fr是法语的缩写。其他语言请百度各国语言缩写即可查询。</p>
<p>（一）应用名称本地化/国际化</p>
<p>应用名称本地化，是指同一个App的名称，在不同的语言环境下（也就是手机设备的语言设置）显示不同的名称。比如，微信在简体中文环境下App名称显示为“微信”，在英语环境下显示为“weChat”。下面就开始进行应用名称本地化。</p>
<p>选中Info.plist，按下键盘上的command + N，选择Strings File（iOS-&gt;Resource-&gt;Strings File）</p>
<p>1055199-974c14fac9fe0083.png</p>
<p>文件名字命名为InfoPlist，且必须是这个名字（因每个人电脑设置差异，此处本人电脑没有显示strings后缀名）:</p>
<p>1055199-4a7f26d3881b7598.png</p>
<p>点击create后，Xcode左侧导航列表就会出现名为InfoPlist.strings的文件，如下图：</p>
<p>1055199-35cac81020746618.png</p>
<p>选中InfoPlist.strings，在Xcode的File inspection（Xcode右侧文件检查器）中点击Localize，目的是选择我们需要本地化的语言，如下图：</p>
<p>注意：在点击Localize之前，一定要保证我们已经添加了需要本地化的语言，也就是上面配置需要国际化的语言那一步（步骤：project-&gt;Info-&gt;Localizations，然后点击”+”，添加需要国际化/本地化的语言）。</p>
<p>1055199-871c2853254ea8b5.png</p>
<p>点击Localize后，会弹出一个对话框，展开对话框列表，发现下拉列表所展示的语言正是我们在上面配置的需要国际化的语言，选择我们需要本地化的语言，然后点击对话框的Localize按钮，如下图：</p>
<p>1055199-034e2065ed6ea719.png</p>
<p>1055199-6d515d6e77ecdf51.png</p>
<p>注意：如果我们没有在 PROJECT 中配置需要国际化的语言（project-&gt;Info-&gt;Localizations，然后点击”+”），上图下拉列表中将只会出现”Base”和”English”选项，English语言是系统默认的语言，其他需要国际化的语言（例如中文简体、法语）必须通过上面的配置本地化语言那一步手动添加。</p>
<p>然后我们发现Xcode右侧的File inspection变成了下图的样式：</p>
<p>1055199-3d54bb8c6a5913a2.png</p>
<p>接下来，勾选French、Chinese（zh-Hans）、Chinese（zh-Hant）、Korean，如下图：</p>
<p>1055199-2560826ab667068d.png</p>
<p>此时，Xcode左侧的InfoPlist.stirings左侧多了一个箭头，点击箭头可以展开，如下图所示：</p>
<p>1055199-7ffacf46ffd88552.png</p>
<p>从上图可以看出，InfoPlist.strings文件下包含了English、French、Chinese（Simplified）、Chinese（Traditional）、Korean这五种语言的文件。</p>
<p>原理：程序启动时，会根据操作系统设置的语言，自动加载InfoPlist.strings文件下对应的语言文件，然后显示应用程序的名字。</p>
<p>接下来，我们分别用不同的语言给InfoPlist.strings下的文件设置对应的名字。</p>
<p>（1）在InfoPlist.strings(english)中加入如下代码：</p>
<p>1</p>
<p>2</p>
<p>// Localizable App Name是App在英语环境环境下显示的名称</p>
<p>CFBundleDisplayName = “Localizable App Name”;<br>备注：CFBundleDisplayName可以使用双引号，也可以不使用双引号！</p>
<p>1055199-011b5c192d0f8e88.png</p>
<p>（2）在InfoPlist.strings(French)中加入如下代码：</p>
<p>1</p>
<p>CFBundleDisplayName = “Le nom de la localisation de l’App”;<br>1055199-6ca3075564e0cc15.png</p>
<p>（3）在InfoPlist.strings(Chinese(Simplified))中加入如下代码：</p>
<p>1</p>
<p>CFBundleDisplayName = “国际化App名称”;<br>1055199-6eadf20b1782286a.png</p>
<p>（4）在InfoPlist.strings(Chinese(Traditional))中加入如下代码：</p>
<p>1</p>
<p>CFBundleDisplayName = “國際化App名稱”;<br>1055199-635b1a8e9abdca4b.png</p>
<p>（5）在InfoPlist.strings(Korean)中加入如下代码：</p>
<p>QQ截图20170122150821.png</p>
<p>1.png</p>
<p>修改模拟器语言环境为English。App名称如下图：</p>
<p>QQ截图20170122151052.png</p>
<p>修改模拟器语言环境为Chinese(Simplified)。App名称如下图：</p>
<p>QQ截图20170122151117.png</p>
<p>修改模拟器语言环境为Chinese(Traditional)。App名称如下图：</p>
<p>QQ截图20170122151142.png</p>
<p>修改模拟器语言环境为Franch。App名称如下图：</p>
<p>QQ截图20170122151202.png</p>
<p>修改模拟器语言环境为Korean。App名称如下图：</p>
<p>QQ截图20170122151221.png</p>
<p>备注：过去本地化App名称，需要在Info.plist文件中增加一个名为“Application has localized display name”的BOOL类型的Key，并且需要将其值设置为YES（如下图）。目的是让App支持本地化App名称。但现在可以忽略这一步。</p>
<p>1055199-38b7885292b31835.png</p>
<p>至此，本地化App名称已经演示完毕，其步骤就是：</p>
<p>在Project的设置中通过点击”+”添加需要本地化的语言。<br>然后在Xcode右侧的File inspection中点击Localize，选中需要本地化App名称的语言。<br>最后在每个语言对应的文件中以key = value(CFBundleDisplayName = “App名称”;);的形式设置App的名称。</p>
<p>所谓字符串本地化，就是指App内的字符串在不同的语言环境下显示不同的内容。比如，”主页”这个字符串在中文语言环境下显示“主页”，在英语环境下显示“home”。下面就开始进行字符串本地化。</p>
<p>其实字符本地化和App名称本地化过程如出一辙，只是创建的文件名成不一样（连同后缀一起，文件名必须是Localizable.strings），其他步骤完全相同。为了能够让大家彻底了解，此处还是会把步骤一一贴出来。</p>
<p>和应用名称本地化一样，首先需要command + N，选择iOS -&gt; Resource -&gt; Strings File</p>
<p>11.png</p>
<p>文件名必须命名为Localizable</p>
<p>备注：因本人电脑取消隐藏文件后缀名，所以会自动补全.strings后缀名。</p>
<p>12.png</p>
<p>文件创建成功，查看Xcode左侧导航列表，发现多了一个名为Localizable.strings的文件，如下图：</p>
<p>13.png</p>
<p>选中Localizable.strings文件，在Xcode的File inspection中点击Localize，目的是选择我们需要本地化的语言（和本地化App名称同理），如下图：</p>
<p>14.png</p>
<p>依次选择English-&gt;Localize，如下图：</p>
<p>15.png</p>
<p>然后我们发现Xcode右侧的File inspection变成了下图的样式：</p>
<p>16.png</p>
<p>然后勾选French、Chinese（zh-Hans）、Chinese（zh-Hant）、Korean，如下图：</p>
<p>17.png</p>
<p>此时，Xcode左侧的Localizable.stirings左侧多了一个箭头，展开后，如下图所示：</p>
<p>18.png</p>
<p>然后我们只需要在Localizable.strings下对应的文件中，分别以Key-Value的形式，为代码中每一个需要本地化的字符串赋值，如下图：</p>
<p>19.png</p>
<p>20.png</p>
<p>21.png</p>
<p>22.png</p>
<p>23.png</p>
<p>本地化代码中的字符串，如下图：</p>
<p>24.png</p>
<p>我们只需要使用Foundation框架自带的NSLocalizedString(key, comment)这个宏根据Key获取对应的字符串，然后赋值给代码中的字符串。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br></pre></td><td class="code"><pre><span class="line">&#x2F;&#x2F; NSLocalizedString(key, comment) 本质</span><br><span class="line"></span><br><span class="line">&#x2F;&#x2F; NSlocalizeString 第一个参数是内容,根据第一个参数去对应语言的文件中取对应的字符串，第二个参数将会转化为字符串文件里的注释，可以传nil，也可以传空字符串@&quot;&quot;。</span><br><span class="line"></span><br><span class="line">#define NSLocalizedString(key, comment) [[NSBundle mainBundle] localizedStringForKey:(key) value:@&quot;&quot; table:nil]&#96;&#96;&#96;</span><br><span class="line">不同语言环境下运行效果，如下图：</span><br><span class="line"></span><br><span class="line">英语环境下：</span><br><span class="line"></span><br><span class="line">25.png</span><br><span class="line"></span><br><span class="line">法语环境下：</span><br><span class="line"></span><br><span class="line">26.png</span><br><span class="line"></span><br><span class="line">简体中文环境下：</span><br><span class="line"></span><br><span class="line">27.png</span><br><span class="line"></span><br><span class="line">繁体中文环境下：</span><br><span class="line"></span><br><span class="line">28.png</span><br><span class="line"></span><br><span class="line">韩语环境下：</span><br><span class="line"></span><br><span class="line">09.png</span><br><span class="line"></span><br><span class="line">如此一来，我们就实现了代码中字符串的本地化。</span><br><span class="line"></span><br><span class="line">技巧</span><br><span class="line"></span><br><span class="line">其实，我们不需要给Localizable.strings(English)文件添加Key-Value。原因如下：系统根据某个key去获取对应的字符串时，如果没有找到，那么就会以key作为value返回。所以如果我们在Localizable.strings(English)文件中没有添加 click &#x3D; &quot;hit&quot;;那么在english环境下btn最终显示的title会是click.</span><br><span class="line">切换语言无需在模拟器中设置，只需要在Xcode中进行如下设置： Edit-&gt;Scheme-&gt;Run-&gt;Arguments Passed On Launch -&gt;-AppleLanguages (语言代码)。例如，我们模拟器此时虽然是韩语，如果通过上述步骤设置为zh-Hans，那么语言环境将会变为汉语，如下图：</span><br><span class="line">29.png</span><br><span class="line"></span><br><span class="line">30.png</span><br><span class="line"></span><br><span class="line">运行效果：</span><br><span class="line"></span><br><span class="line">31.png</span><br><span class="line"></span><br><span class="line">如此一来，切换语言变得更加简单，无需在模拟器的设置中进行繁琐的语言切换。</span><br><span class="line"></span><br><span class="line">（三）多人开发情况下的字符串本地化</span><br><span class="line"></span><br><span class="line">项目开发中，独立开发的还是少数。经常会有多人开发的情况，这种情况，如果多人同时操作本地化文件，极有可能会存在冲突。另一方面，我们又不希望自己的本地化文件受到对方的污染，也就是说，我们不希望对方操作我们的本地化文件。但是上面介绍的代码中字符串的本地化是使用的是默认的文件名&quot;Localizable&quot;,因为启动程序时，系统将根据语言加载相应的文件得到其对应的字符串文件，这个字符串可以通过系统将NSLocalizedString中的宏生成名为“Localizable.strings”的文件。那么如何让系统加载我们自己命名的本地化文件而非系统默认的Localizable.strings呢？这就是 NSLocalizedStringFromTable(&lt;#key#&gt;, &lt;#tbl#&gt;, &lt;#comment#&gt;)的用处。</span><br><span class="line"></span><br><span class="line">也就是说，如果你的strings文件名字不是Localizable而是自定义的话，如VVS.strings，那么你就得使用NSLocalizedStringFromTable这个宏来读取本地化字符串。</span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure>
<p>// key：系统根据key取字符串</p>
<p>// tbl：自定义strings文件的名字</p>
<p>// comment：可以不传</p>
<pre><code>NSLocalizedStringFromTable(, , )
</code></pre>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">32.png</span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure>
<p>#import “ViewController.h”</p>
<p>@interface ViewController ()</p>
<p>@property (weak, nonatomic) IBOutlet UIButton *btn;</p>
<p>@end</p>
<p>@implementation ViewController</p>
<ul>
<li><p>(void)viewDidLoad {</p>
<p>  [super viewDidLoad];</p>
<p>  NSString *title = NSLocalizedStringFromTable(@”click”, @”VVS”, nil);</p>
<p>  [self.btn setTitle:title forState:UIControlStateNormal];</p>
</li>
</ul>
<p>}</p>
<p>@end</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">运行效果，如下图：</span><br><span class="line"></span><br><span class="line">33.png</span><br><span class="line"></span><br><span class="line">如此一来，我们就可以挣脱别人的strings文件和系统默认的Localizable.strings文件，自己另起炉灶。</span><br><span class="line"></span><br><span class="line">（四）图片本地化</span><br><span class="line"></span><br><span class="line">本地化图片，有两种方式，第一种方式和本地化代码中的字符串一样，通过NSLocalizedString(key,comment)来获取相应的字符串，然后根据这个字符串再获取图片。</span><br><span class="line"></span><br><span class="line">方式一</span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure>
<p>NSString *imageName = NSLocalizedString(@”icon”, nil);</p>
<p>UIImage *image = [UIImage imageNamed:imageName];</p>
<p>self.imageView.image = image;</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><span class="line">方式二</span><br><span class="line">首先需要添加需要本地化的语言，具体步骤参考第一章配置需要国际化的语言。因为我演示的demo中在本地化App名称时已经添加了需要国际化的语言。所以不需要再设置。</span><br><span class="line"></span><br><span class="line">将图片拖入工程中，例如“icon.png”，然后选中icon.png，展开Xcode右侧的file Inspection，点击Localize，如下图:</span><br><span class="line"></span><br><span class="line">34.png</span><br><span class="line"></span><br><span class="line">然后，右击icon.png-&gt;show in Finder，我们发现在en.Iproj文件中多了一个名为icon.png的图片，如下图：</span><br><span class="line"></span><br><span class="line">35.png</span><br><span class="line"></span><br><span class="line">当然，zh-Hans.Iproj文件夹下并没有图片，如下图：</span><br><span class="line"></span><br><span class="line">36.png</span><br><span class="line"></span><br><span class="line">我们只需给zh-Hans.Iproj添加一个名字也为icon.png的图片。如下图：</span><br><span class="line"></span><br><span class="line">37.png</span><br><span class="line"></span><br><span class="line">然后把zh-Hans.Iproj中的icon.png拖到Xcode中，如下图：</span><br><span class="line"></span><br><span class="line">38.png</span><br><span class="line"></span><br><span class="line">然后发现，icon.png左边出现了一个可以展开的三角形。如下图：</span><br><span class="line"></span><br><span class="line">39.png</span><br><span class="line"></span><br><span class="line">展开后发现里面包含两张图片，如下图：</span><br><span class="line"></span><br><span class="line">40.png</span><br><span class="line"></span><br><span class="line">然后控制器中添加如下代码：</span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure>
<p>#import “ViewController.h”</p>
<p>@interface ViewController ()</p>
<p>@property (weak, nonatomic) IBOutlet UIImageView *imageView;</p>
<p>@end</p>
<p>@implementation ViewController</p>
<ul>
<li><p>(void)viewDidLoad {</p>
<p>  [super viewDidLoad];</p>
<p>  NSString *imageName = NSLocalizedString(@”icon”, nil);</p>
<p>  UIImage *image = [UIImage imageNamed:imageName];</p>
<p>  self.imageView.image = image;</p>
</li>
</ul>
<p>}</p>
<p>@end</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line">english环境下运行效果如下：</span><br><span class="line"></span><br><span class="line">QQ截图20170122153125.png</span><br><span class="line"></span><br><span class="line">中文环境改下运行效果如下：</span><br><span class="line"></span><br><span class="line">41.png</span><br><span class="line"></span><br><span class="line">其他语言环境，例如法语、中文繁体、韩语，操作步骤完全和上面相同。都是在对应的.Iproj文件夹下添加同名的图片，然后把图片拖放到Xcode中，不再赘述。</span><br><span class="line"></span><br><span class="line">（五）查看&#x2F;切换本地语言</span><br><span class="line"></span><br><span class="line">原理：应用启动时，首先会读取NSUserDefaults中的key为AppleLanguages对应的value，该value是一个String数组，也就是说，我们访问这个名为AppleLanguages的key可以返回一个string数组，该数组存储着APP支持的语言列表，数组的第一项为APP当前默认的语言。</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure>
<p>#import “ViewController.h”</p>
<p>@interface ViewController ()</p>
<p>@end</p>
<p>@implementation ViewController</p>
<ul>
<li><p>(void)viewDidLoad {</p>
<p>  [super viewDidLoad];</p>
<p>  NSArray *languages = [[NSUserDefaults standardUserDefaults] valueForKey:@”AppleLanguages”];</p>
<p>  NSString *currentLanguage = languages.firstObject;</p>
<p>  NSLog(@”模拟器当前语言：%@”,currentLanguage);</p>
</li>
</ul>
<p>}</p>
<p>@end</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">-控制台打印结果：</span><br><span class="line"></span><br><span class="line">1055199-5f3eaa46f8afd678.png</span><br><span class="line"></span><br><span class="line">同理，既然我们可以通过AppleLanguages这个key从NSUserDefaults中取出语言数组，那么我们也可以给AppleLanguages这个key赋值来达到切换本地语言的效果，从此以后，我们就无需频繁的去模拟器的设置-&gt;通用-&gt;语言与地区 中切换语言。</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure>
<p>#import “ViewController.h”</p>
<p>@interface ViewController ()</p>
<p>@end</p>
<p>@implementation ViewController</p>
<ul>
<li><p>(void)viewDidLoad {</p>
<p>  [super viewDidLoad];</p>
<p>  // 切换语言前</p>
<p>  NSArray *langArr1 = [[NSUserDefaults standardUserDefaults] valueForKey:@”AppleLanguages”];</p>
<p>  NSString *language1 = langArr1.firstObject;</p>
<p>  NSLog(@”模拟器语言切换之前：%@”,language1);</p>
</li>
</ul>
<pre><code>// 切换语言

NSArray *lans = @[@&quot;en&quot;];

[[NSUserDefaults standardUserDefaults] setObject:lans forKey:@&quot;AppleLanguages&quot;];




// 切换语言后

NSArray *langArr2 = [[NSUserDefaults standardUserDefaults] valueForKey:@&quot;AppleLanguages&quot;];

NSString *language2 = langArr2.firstObject;

NSLog(@&quot;模拟器语言切换之后：%@&quot;,language2);
</code></pre>
<p>}</p>
<p>@end</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">1055199-1ca3ab123ed3927e.png</span><br><span class="line"></span><br><span class="line">回想一下，我在上面的技巧一节中提过：切换语言无需在模拟器中设置，只需要在Xcode中进行如下设置： Edit-&gt;Scheme-&gt;Run-&gt;Arguments Passed On Launch -&gt;-AppleLanguages (语言代码)。其实本质上就是给NSUserDefaults中名为AppleLanguages的key赋值。</span><br><span class="line"></span><br></pre></td></tr></table></figure>
    </div>

    
    
    

      <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/iOS/" rel="tag"># iOS</a>
          </div>

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/d1de390.html" rel="prev" title="Hexo设置永久链接">
      <i class="fa fa-chevron-left"></i> Hexo设置永久链接
    </a></div>
      <div class="post-nav-item">
    <a href="/b1e125e5.html" rel="next" title="环信IM开发笔记">
      环信IM开发笔记 <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="YAND"
      src="/images/avatar.jpg">
  <p class="site-author-name" itemprop="name">YAND</p>
  <div class="site-description" itemprop="description">成长历程 IT笔记 黑苹果 杂谈 建站 服务器 VPS</div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">19</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/categories/">
          
        <span class="site-state-item-count">6</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/tags/">
          
        <span class="site-state-item-count">6</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author motion-element">
      <span class="links-of-author-item">
        <a href="https://github.com/vvvc" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;vvvc" rel="noopener" target="_blank"><i class="fa fa-fw fa-github"></i>GitHub</a>
      </span>
      <span class="links-of-author-item">
        <a href="mailto:d.zt@qq.com" title="E-Mail → mailto:d.zt@qq.com" rel="noopener" target="_blank"><i class="fa fa-fw fa-envelope"></i>E-Mail</a>
      </span>
  </div>


  <div class="links-of-blogroll motion-element">
    <div class="links-of-blogroll-title">
      <i class="fa fa-fw fa-link"></i>
      Links
    </div>
    <ul class="links-of-blogroll-list">
        <li class="links-of-blogroll-item">
          <a href="http://vxgo.cn/" title="http:&#x2F;&#x2F;vxgo.cn" rel="noopener" target="_blank">不语玩卡</a>
        </li>
    </ul>
  </div>

      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        
  <div class="beian"><a href="http://www.beian.miit.gov.cn/" rel="noopener" target="_blank">京ICP备20023063号 </a>
  </div>

<div class="copyright">
  
  &copy; 2016 – 
  <span itemprop="copyrightYear">2021</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">YAND</span>
</div>

        








      </div>
    </footer>
  </div>

  
  <script src="/lib/anime.min.js"></script>
  <script src="/lib/velocity/velocity.min.js"></script>
  <script src="/lib/velocity/velocity.ui.min.js"></script>

<script src="/js/utils.js"></script>

<script src="/js/motion.js"></script>


<script src="/js/schemes/pisces.js"></script>


<script src="/js/next-boot.js"></script>




  















  

  

</body>
</html>
